part of 'page.dart';

class _WebPurchaseItemPageWloc
    extends StatefulWloc<_WebPurchaseItemPageWlocState, WebPurchaseItemPage>
    with SingleTickerProviderStateMixin {
  late final WebPurchaseItemPageBloc _bloc;

  @override
  _WebPurchaseItemPageWlocState get initialState {
    return _WebPurchaseItemPageWlocStateFactory().create(
      blocState: _bloc.state,
    );
  }

  void onPurchaseItemTilePressed({
    required String purchaseItemSku,
  }) {
    _bloc.selectPurchaseItem(
      purchaseItemSku: purchaseItemSku,
    );
  }

  void requestPurchase() {
    _bloc.requestPurchase();
  }

  void onHeaderLogoButtonPressed() {
    _bloc.openHomePage();
  }

  void onHeaderAngryWordsProButtonPressed() {
    _bloc.openWebPurchaseItemPage();
  }

  void onHeaderSigningInButtonPressed() {
    _bloc.openSigningInPage();
  }

  void onHeaderSigningUpButtonPressed() {
    _bloc.openSigningUpPage();
  }

  void onHeaderUserActionDialogAccountTilePressed() {
    _bloc.openAccountPage();
  }

  void onHeaderUserActionDialogSigningOutTilePressed() {
    _bloc.requestSigningOut();
  }

  void _onStateChanged({
    required WebPurchaseItemPageBlocState blocState,
  }) {
    final updatedState = _WebPurchaseItemPageWlocStateFactory().create(
      blocState: blocState,
    );

    emit(updatedState);
  }

  @override
  void initState() {
    super.initState();

    _bloc = WebPurchaseItemPageBloc(
      urlLauncherBloc: core_di.sl(),
      appNavigatorBloc: core_di.sl(),
      authBloc: core_di.sl(),
      getWebPurchaseItemModels: home_di.sl(),
      placeOrder: home_di.sl(),
      signOut: core_di.sl(),
    );

    _bloc.load();
  }

  @override
  void dispose() {
    _bloc.close();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return BlocListener<WebPurchaseItemPageBloc, WebPurchaseItemPageBlocState>(
      bloc: _bloc,
      listener: (context, state) {
        _onStateChanged(
          blocState: state,
        );
      },
      child: _WebPurchaseItemPageView(
        wloc: this,
      ),
    );
  }
}
